package com.baijiaoxi.common.utils.spring;

import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;

import java.util.Map;

public interface RestTemplateClient {
    interface Interceptor {
        void beforeRequest(HttpHeaders httpHeaders);

    }

    default HttpHeaders createHeaders(MediaType mediaType, Map<String, String> headersMap) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(mediaType);
        if (headersMap != null && !headersMap.isEmpty())
            headersMap.forEach((k, v) -> headers.set(k, v));
        return headers;
    }

    default HttpHeaders createHeaders(Map<String, String> headersMap) {
        return createHeaders(MediaType.APPLICATION_JSON_UTF8, headersMap);
    }

    default <T> HttpEntity<T> createHttpEntity(HttpHeaders httpHeaders, T body) {
        if (body != null)
            return new HttpEntity<T>(body, httpHeaders);
        else return new HttpEntity<T>(httpHeaders);
    }

    /**
     * post
     */

    <T> T post(String url, Object json, Class<T> responseType, Map<String, String> headersMap);

    <T> T post(String url, Object json, ParameterizedTypeReference<T> responseType, Map<String, String> headersMap);

    default <T> T post(String url, Object json, Class<T> responseType) {
        return post(url, json, responseType, null);
    }

    default <T> T post(String url, Object json, ParameterizedTypeReference<T> responseType) {
        return post(url, json, responseType, null);
    }

    /**
     * put
     */
    <T> T put(String url, Object json, Class<T> responseType, Map<String, String> headersMap);

    <T> T put(String url, Object json, ParameterizedTypeReference<T> responseType, Map<String, String> headersMap);

    default <T> T put(String url, Object json, Class<T> responseType) {
        return put(url, json, responseType, null);
    }

    default <T> T put(String url, Object json, ParameterizedTypeReference<T> responseType) {
        return put(url, json, responseType, null);
    }

    /**
     * delete
     */
    <T> T delete(String url, Object json, Class<T> responseType, Map<String, String> headersMap);

    <T> T delete(String url, Object json, ParameterizedTypeReference<T> responseType, Map<String, String> headersMap);

    default <T> T delete(String url, Object json, Class<T> responseType) {
        return delete(url, json, responseType, null);
    }

    default <T> T delete(String url, Object json, ParameterizedTypeReference<T> responseType) {
        return delete(url, json, responseType, null);
    }

    /**
     * get
     */
    <T> T get(String url, Object json, Class<T> responseType, Map<String, String> headersMap);

    <T> T get(String url, Object json, ParameterizedTypeReference<T> responseType, Map<String, String> headersMap);

    default <T> T get(String url, Object json, Class<T> responseType) {
        return get(url, json, responseType, null);
    }

    default <T> T get(String url, Class<T> responseType) {
        return get(url, null, responseType, null);
    }

    default <T> T get(String url, Class<T> responseType, Map<String, String> headersMap) {
        return get(url, null, responseType, headersMap);
    }

    default <T> T get(String url, Object json, ParameterizedTypeReference<T> responseType) {
        return get(url, json, responseType, null);
    }

    default <T> T get(String url, ParameterizedTypeReference<T> responseType) {
        return get(url, null, responseType, null);
    }

    default <T> T get(String url, ParameterizedTypeReference<T> responseType, Map<String, String> headersMap) {
        return get(url, null, responseType, headersMap);
    }

    /**
     * postFile
     */
    <T> T postFile(String url, String filepath, String formParamName, Class<T> responseType, Map<String, String> headersMap);

    <T> T postFile(String url, String filepath, String formParamName, ParameterizedTypeReference<T> responseType, Map<String, String> headersMap);

    default <T> T postFile(String url, String filepath, Class<T> responseType) {
        return postFile(url, filepath, "file", responseType, null);
    }

    default <T> T postFile(String url, String filepath, Class<T> responseType, Map<String, String> headersMap) {
        return postFile(url, filepath, "file", responseType, headersMap);
    }

    default <T> T postFile(String url, String filepath, String formParamName, Class<T> responseType) {
        return postFile(url, filepath, formParamName, responseType, null);
    }

    default <T> T postFile(String url, String filepath, ParameterizedTypeReference<T> responseType) {
        return postFile(url, filepath, "file", responseType, null);
    }

    default <T> T postFile(String url, String filepath, ParameterizedTypeReference<T> responseType, Map<String, String> headersMap) {
        return postFile(url, filepath, "file", responseType, headersMap);
    }

    default <T> T postFile(String url, String filepath, String formParamName, ParameterizedTypeReference<T> responseType) {
        return postFile(url, filepath, formParamName, responseType, null);
    }


}
